package nl.hanze.web.homegrownrpc.hello;

import nl.hanze.web.homegrownrpc.generic.*;
import java.net.*;
import java.io.*;
import sun.misc.*;

public class HelloStub implements Hello, Stub {
    private String strIP;
    private int port;
	
    public void setSkelLocation(String strIP, int port) throws Exception {
        this.strIP=strIP;
        this.port=port;

    }

    public String sayHello() throws Exception {
        Socket socHelloStub=new Socket(strIP, port);
        OutputStreamWriter oswHelloStub=new OutputStreamWriter(socHelloStub.getOutputStream());
        BufferedReader bufHelloStub=new BufferedReader(new InputStreamReader(socHelloStub.getInputStream()));
        oswHelloStub.write("sayHello#0\n");
        oswHelloStub.flush();
        String strResult=bufHelloStub.readLine();
        String value=new String(new BASE64Decoder().decodeBuffer(strResult.split("#")[1]));
        return value;
    }

    public String sayHello(String name) throws Exception {
        Socket socHelloStub=new Socket(strIP, port);
        OutputStreamWriter oswHelloStub=new OutputStreamWriter(socHelloStub.getOutputStream());
        BufferedReader bufHelloStub=new BufferedReader(new InputStreamReader(socHelloStub.getInputStream()));
        String param1=new BASE64Encoder().encode(name.getBytes());
        oswHelloStub.write("sayHello#1#java.lang.String#"+param1+"\n");
        oswHelloStub.flush();
        String strResult=bufHelloStub.readLine();
        String value=new String(new BASE64Decoder().decodeBuffer(strResult.split("#")[1]));
        return value;
    }

    /**
     * Implements Hello's sayHello(int age) by sending the arguments to the
     * remotely hosted method and returning the answer it receives back.
     * 
     * @param age           The age to use (int)
     * @return              A string produced with the arguments
     * @throws Exception    Throws UnknownHostException when host for
     *                      webservice can't be found
     */
    public String sayHello(int age) throws Exception {
        // First set up the network-connection to the hosted web service
        Socket socHelloStub=new Socket(strIP, port);
        OutputStreamWriter oswHelloStub=new OutputStreamWriter(socHelloStub.getOutputStream());
        BufferedReader bufHelloStub=new BufferedReader(new InputStreamReader(socHelloStub.getInputStream()));
        // Then send the request for method over the network using the protocol
        String param1=""+age;
        oswHelloStub.write("sayHello#1#int#"+param1+"\n");
        oswHelloStub.flush();
        // Lastly read the response and return the produced result
        String strResult=bufHelloStub.readLine();
        String value=new String(new BASE64Decoder().decodeBuffer(strResult.split("#")[1]));
        return value;
    }

    /**
     * Implements Hello's sayHello(String name, int age) by sending the arguments
     * to the remotely hosted method and returning the answer it receives back.
     * 
     * @param name          The name to use (String)
     * @param age           The age to use (int)
     * @return              A string produced with the arguments
     * @throws Exception    Throws UnknownHostException when host for
     *                      webservice can't be found
     */
    public String sayHello(String name, int age) throws Exception {
        // First set up the network-connection to the hosted web service
        Socket socHelloStub=new Socket(strIP, port);
        OutputStreamWriter oswHelloStub=new OutputStreamWriter(socHelloStub.getOutputStream());
        BufferedReader bufHelloStub=new BufferedReader(new InputStreamReader(socHelloStub.getInputStream()));
        // Then send the request for method over the network using the protocol
        String param1=new BASE64Encoder().encode(name.getBytes());
        String param2=""+age;
        oswHelloStub.write("sayHello#2#java.lang.String#int#"+param1+"#"+param2+"\n");
        oswHelloStub.flush();
        // Lastly read the response and return the produced result
        String strResult=bufHelloStub.readLine();
        String value=new String(new BASE64Decoder().decodeBuffer(strResult.split("#")[1]));
        return value;
    }

    /**
     * Implements Hello's ageNextYear(int age) by sending the arguments to the
     * remotely hosted method and returning the answer it receives back.
     * 
     * @param age           The initial age to use (int)
     * @return              An int with the calculated age
     * @throws Exception    Throws UnknownHostException when host for
     *                      webservice can't be found
     */
    public int ageNextYear(int age) throws Exception {
        // First set up the network-connection to the hosted web service
        Socket socHelloStub=new Socket(strIP, port);
        OutputStreamWriter oswHelloStub=new OutputStreamWriter(socHelloStub.getOutputStream());
        BufferedReader bufHelloStub=new BufferedReader(new InputStreamReader(socHelloStub.getInputStream()));
        // Then send the request for method over the network using the protocol
        String param1=""+age;
        oswHelloStub.write("ageNextYear#1#int#"+param1+"\n");
        oswHelloStub.flush();
        // Lastly read the response and return the produced result
        String strResult=bufHelloStub.readLine();
        int value=Integer.parseInt(strResult.split("#")[1]);
        return value;
    }
}